require(['./config'],() => {

	require(['request','template','header','greycar','jquery_zoom','footer','jquery_fly'],(request,template,header,greycar,jquery_zoom) =>{
		class Cake{
			constructor() {
			    this.getcake().then( () => {
					this.addtocar()
					this.zoom()
					this.numadd()
					this.numreduce()
					header.calcarcount()
					 greycar.calcarcoun()	
					 this.a = $('#num').val()
				})
				
			}
			getcake (){
				const id = location.search.slice(1)
				console.log(id)
				return new Promise(resolve => {
					request.getcake(id).then(
					resp => {
						if (resp.code === 200){
							const list = resp
							const src = resp.src[0]
							const url = resp.src
							const {id,fenlei,num,tittle} = resp
							this.cake = {id,fenlei,num,tittle,src,url}
							
							$('#cake_show_body').html(template('cake_Template',{list,url}))
							$('#cake_details_ul').html(template('cake_xiangqing_Template',{list,url}))
							resolve()
						}
					})
				})
				
			}
			
			numreduce(){
						  
						  $('#right_cake_reduce').on('click',()=>{
							if(this.a>0) {
								 this.a --
								 $('#num').val(this.a)
								 
							}
							console.log(this.a)
						  })
			}
			numadd(){
						  
								console.log($('#right_cake_add'))  
						  $('#right_cake_add').on('click',()=>{
							 this.a ++
		                   console.log(this.a)
						   $('#num').val(this.a)
						  })
			}
			
			addtocar(){
				
					console.log(this.a)
				$('#addtocar').on('click',(e) => {
					
					this.jqfly(e)
					let cart = localStorage.getItem('cart')
					if(cart) {
						cart = JSON.parse(cart)
						// 购物车以及有数据
						
						const isExist = cart.some(shop => shop.id === this.cake.id)
						if(isExist){
							// 当前商品已经加入购物车
							cart = cart.map(shop => {
								if(shop.id === this.cake.id) {shop.count = parseFloat(shop.count )+ parseFloat(this.a)
								}
								return shop
							})
						}else{
							// 当前商品第一次加入购物车
							cart.push({
								...this.cake,
								count:this.a,
								check:true
							})
						}
						localStorage.setItem('cart',JSON.stringify(cart))
					}
					else{
						localStorage.setItem('cart',JSON.stringify([{
							...this.cake,
							count:this.a,
							check:true
							}]))
					}
					header.calcarcount()
				})
			}
			zoom(){
				$(".zoom-img").elevateZoom({
							gallery:"show_img_list",
							cursor:"crosshair",
							// cursor:"pointer",crosshair
							galleryActiveClass:"active",
							borderSize:"0",
							lensSize: 200,
							zoomWindowWidth:"500",
							zoomWindowHeight:"500",

							})
			}
			jqfly(e){
				       
                 console.log(5)
                		  $('<div class="active"></div>').fly({
                		  start:{
                				left: e.clientX,  //开始位置（必填）#fly元素会被设置成position: fixed
                				top: e.clientY,  //开始位置（必填）
                			  },
                			  end:{
                				left: window.innerWidth-20, //结束位置（必填）
                				top: 200,  //结束位置（必填）
                			   
                			  }
                
                		  
                		  })
			
			 }
		}
		new Cake()
	})
	
			   
			   
			   })
			   
			   
				   				 
				   				  	 
			 
	
	
